<template>
	<view class="container">
		<uni-segmented-control :current="current" :values="items" style-type="text" active-color="#FA2C1E"
		 @clickItem="onClickItem"  style="background-color: #fff;"/>
		<view class="card_active" v-if="current==0">
			<view class="item">
				<view class="lable">
					激活卡号
				</view>
				<view class="value">
					<input v-model="codeid" type="text"  placeholder="请输入激活卡号" style="padding-left: 20rpx;border-radius: 16rpx;"/>
				</view>
			</view>
			<view class="item">
				<view class="lable ">
					卡密
				</view>
				<view class="value">
					<!-- :style="{width:inputWid(codepwd)}" -->
					<input v-model="codepwd" @input="onKeyInput" type="text" class="kami"/>
				</view>
			</view>
			<button class="btn" @click="activeCard">确认激活</button>
		</view>
		
		<view class="zs" v-if="current==1">
			 <view class="list">
			 	<view class="card other_info"  v-for="(item,index) in cardList" :key="index">
			 		<view class="card_content">
			 			<view class="cell">
			 				<view class="label"> 激活时间 </view>
			 				<view class="value"> {{item.activationtime}} </view>
			 			</view>
			 			<view class="cell">
			 				<view class="label"> 产品名称 </view>
			 				<view class="value"> {{item.cardlibrary.name}} </view>
			 			</view>
			 			<view class="cell">
			 				<view class="label"> 卡号 </view>
			 				<view class="value"> {{item.codeid}} </view>
			 			</view>
						<!-- <view class="cell">
							<view class="label"> 激活绑定手机号 </view>
							<view class="value"> 15038215257 </view>
						</view> -->
			 		</view>
					<view class="card_bottom">
						<button class="btn" @click="toActiveDetail">查看激活订单</button>
					</view>
			 	</view>
			 </view>
			 <uni-load-more :status="status" :content-text="contentText" />
		</view>
		<uni-popup ref="popup" type="center" :animation="false" :mask-click="false" >
			<view  class="popup">
				<view class="close"  @click="closePopup">
					<image src="/static/icon_close.png" mode="widthFix"></image>
				</view>
				<view class="title">
					激活提示
				</view>
				<view class="center">
					<image :src="resultImage[result]" mode="widthFix" style="width: 134rpx;height: 156rpx;"></image>
					<view class="msg">
						{{resultMsg}}
					</view>
				</view>
				<button class="btn" @click="closePopup">{{resultBtnText}}</button>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {activation,mycode} from '../../../api/api.js'
	export default {
		data() {
			return {
				current: 0,
				items: ['卡片激活', '激活记录'],
				page:1,
				codeid:'',
				codepwd:'',
				cardList:[],
				status:'more',
				statusTypes: [{
					value: 'more',
					text: '加载前',
					checked: true
				}, {
					value: 'loading',
					text: '加载中',
					checked: false
				}, {
					value: 'noMore',
					text: '没有更多',
					checked: false
				}],
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				result:'',
				resultImage:{
					'1':'/static/success.png',
					'0':'/static/fail.png'
				},
				resultMsg:'',
				resultBtnText:'我知道了'
			}
		},
		onLoad(options) {
			this.getList()
		},
		onReachBottom(){  //上拉触底函数
			if(!this.isLoadMore){  //此处判断，上锁，防止重复请求
				this.isLoadMore=true
				this.page+=1
				this.getList()
			}
		},
		computed:{
			// input输入框宽度自适应
			inputWid() {
				return function(value) {
					if (value == '' || value == 0) {
						return '210rpx';
					} else {
						return (String(value).length ) * 24 + 40 + 'rpx';
					}
				};
			}
		},
		methods:{
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
			},
			onKeyInput: function(event) {
				this.codepwd = event.target.value.toUpperCase()
			},
			getList(){
				this.status = 'loading'
				mycode({status:this.current,page:this.page,limit:15}).then(res=>{
					console.log(res)
					this.cardList.push(...res.data.data.data)
					console.log(this.cardList.length,res.data.data.total)
					if(this.cardList.length >= res.data.data.total){
						this.status = 'noMore'
					}else {
						this.status = 'more'
					}
				})
			},
			activeCard(){
				if(this.codeid == ''){
					uni.showToast({
						title:'请输入卡号',
						icon:'none'
					})
					return
				}
				if(this.codepwd == '' ){
					uni.showToast({
						title:'请输入卡密',
						icon:'none'
					})
					return
				}
				
				activation({codeid:this.codeid,codepwd:this.codepwd}).then(res=>{
					this.result = res.data.code
					if(res.data.code == '1'){
						//该卡片激活成功！
						this.resultMsg = '该卡片激活成功！'
						this.resultBtnText = '查看激活记录'
					}else {
						this.resultMsg = res.data.msg || '该卡片激活失败！'
						this.resultBtnText = '知道了'
					}
					this.$refs.popup.open()
					
				})
			},
			closePopup(){
				this.$refs.popup.close()
				if(this.result == '1'){
					this.codeid == ''
					this.codepwd == ''
					this.current = '1'
					this.isLoadMore = true
					this.page = 1
					this.cardList = []
					this.getList()
				}
			},
			toActiveDetail(){
				uni.redirectTo({
					url: '/pages/my/myServices/myServices'
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: calc(100vh - 88rpx);
		background: #F8F8F8;
		/deep/.segmented-control {
			background-color: #fff;
		}
	}
	.card_active {
		padding: 38rpx 30rpx;
		.item {
			display: flex;
			margin-bottom: 28rpx;
			align-items: center;
			.lable {
				text-align: right;
				width: 140rpx;
				margin-right: 28rpx;
			}
			.value {
				width: calc(100% - 168rpx);
				input {
					height: 100rpx;
					background-color: #fff;
				}
				.kami {
					background: #E5E5E5;
					width: 206rpx;
					height: 46rpx;
					padding: 0 20rpx;
					font-size: 28rpx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 500;
					color: #404040;
					line-height: 46rpx;
				}
			}
		}
		.btn {
			margin-top: 40rpx;
			width: 100%;
			height: 88rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: linear-gradient(90deg, #FF4050 0%, #FA2C1E 100%);
			border-radius: 44rpx;
			font-size: 32rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
		}
	}
	.list {
		padding: 0 24rpx;
	}
	.card {
		background: #FFFFFF;
		border-radius: 16rpx;
		padding: 20rpx 30rpx;
		margin-top: 24rpx;
		.card_top {
			height: 82rpx;
			padding-bottom: 20rpx;
			margin-bottom: 10rpx;
			border-bottom: 2rpx solid #E7E7E7;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.card_title {
				font-size: 26rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 600;
				color: #222222;
			}
			.card_title_right {
				font-size: 26rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 600;
				color: #222222;
			}
		}
		.card_content {
			.cell {
				display: flex;
				// align-items: center;
				font-size: 26rpx;
				line-height: 36rpx;
				padding: 10rpx 0;
				.label {
					flex: 1;
					color: #333333;
				}
				.value {
					flex: 2;
					text-align: right;
					color: #222222;
				}
			}
		}
		.card_bottom {
			margin-top: 12rpx;
			.btn {
				height: 60rpx;
				border-radius: 30rpx;
				font-size: 26rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				line-height:60rpx;
				float: right;
				margin-left: 20rpx;
				background: linear-gradient(90deg, #FF4050 0%, #FA2C1E 100%);
				color: #FFFFFF;
			}
			.btn::after{
				content:none;
				border:none
			}
		}
		.card_bottom::after{clear:both;content:'';display:block;}
	}

	.popup {
		width: 85vw;
		border-radius: 16rpx;
		background-color: #fff;
		position: relative;
		.close {
			position: absolute;
			top:44rpx;
			right: 32rpx;
			height: 32rpx;
			width: 32rpx;
			image {
				height: 32rpx;
				width: 32rpx;
			}
		}
		.title {
			padding: 40rpx 0 0 40rpx;
			font-size: 28rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #353535;
		}
		.center {
			margin-top: 60rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			.msg {
				margin-top: 30rpx;
				font-size: 34rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				color: #353535;
			}
		}
		.btn {
			margin-top: 60rpx;
			font-size: 34rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #FA2C1E;
			line-height: 48rpx;
			letter-spacing: 2rpx;
			height: 110rpx;
			border: none;
			border-top: 2rpx solid rgba(0,0,0,0.1);
			display: flex;
			justify-content: center;
			align-items: center;
			background-color: transparent;
		}
		.btn::after{content: none;}
	}
</style>
